#include "gui/transactionviewreportsdialog.h"
#include "core/constants.h"
#include "core/transactioncontroller.h"
#include "core/transactionmodel.h"
#include "core/basemodel.h"

void TransactionViewReportsDialog::initUiComponents()
{
    QFont font;

    mainLayout = new QVBoxLayout;

    headerLabel = new QLabel("Transaction Reports");
    font = headerLabel->font();
    font.setPointSize(font.pointSize() + HEADER_UPSIZE);
    headerLabel->setFont(font);
    mainLayout->addWidget(headerLabel);

    subHeaderLabel = new QLabel("View important information about Transactions");
    mainLayout->addWidget(subHeaderLabel);
    mainLayout->addWidget(new SeparatorWidget);

    unpaidReport = new CollapsibleWidget;
    unpaidReport->setText("Unpaid Transactions");
    unpaidList = new ShinyList;
    unpaidReport->setWidget(unpaidList);
    mainLayout->addWidget(unpaidReport);

    waitingOnStocksReport = new CollapsibleWidget;
    waitingOnStocksReport->setText("Transactions waiting on Stocks");
    waitingOnStocksList = new ShinyList;
    waitingOnStocksReport->setWidget(waitingOnStocksList);
    mainLayout->addWidget(waitingOnStocksReport);

    mainLayout->addStretch();
    mainLayout->addWidget(new SeparatorWidget);

    buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
    mainLayout->addWidget(buttonBox);

    this->setLayout(mainLayout);
}

void TransactionViewReportsDialog::initSignalsAndSlots()
{
    connect(this->buttonBox, SIGNAL(rejected()),
            this, SLOT(reject()));
}

void TransactionViewReportsDialog::generateUnpaidTransactionsReport()
{
    TransactionController ctrl;
    QList<BaseModel *> models = ctrl.getAllUnpaid();
    TransactionModel *transaction;
    ShinyListItem *listItem;
    QString mainText = "Sale made on %1";
    QString subText = "Sold to %1";
    int i;

    for (i = 0; i < models.length(); i++) {
        listItem = new ShinyListItem;
        transaction = (TransactionModel *) models.at(i);
        listItem->setMainText(mainText.arg(
                                  transaction->getSaleTime().toString("ddd MMMM d, yyyy")));
        listItem->setSubText(subText.arg(transaction->getCustomer()->getName()));
        unpaidList->addItem(listItem);
    }
}

void TransactionViewReportsDialog::generateUndeliveredTransactionsReport()
{
    TransactionController ctrl;
    QList<BaseModel *> models = ctrl.getAllUndelivered();
    TransactionModel *transaction;
    ShinyListItem *listItem;
    QString mainText = "Sale made on %1";
    QString subText = "Sold to %1; waiting on stocks";
    int i;

    for (i = 0; i < models.length(); i++) {
        listItem = new ShinyListItem;
        transaction = (TransactionModel *) models.at(i);
        listItem->setMainText(mainText.arg(
                                  transaction->getSaleTime().toString("ddd MMMM d, yyyy")));
        listItem->setSubText(subText.arg(transaction->getCustomer()->getName()));
        waitingOnStocksList->addItem(listItem);
    }
}

TransactionViewReportsDialog::TransactionViewReportsDialog(QWidget *parent) :
    QDialog(parent)
{
    this->setWindowTitle("Transaction Reports");
    this->setMinimumSize(400, 300);
    this->initUiComponents();
    this->initSignalsAndSlots();
    this->generateUnpaidTransactionsReport();
    this->generateUndeliveredTransactionsReport();
}
